﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class LevelItem : MonoBehaviour
{
    public int levelId;
    public Image finishMarkImage;
    public Button button;
    public Text levelText;
    public Image bgImage;
    private int levelState = 0;

    public Color lockColor;
    public Color unLockColor;

    //public 

    private void Start()
    {
        BtnEvent();
    }

    private void BtnEvent()
    {
        button.onClick.AddListener(() =>
        {
            SoundManager.Instance.PlayAudio(SoundType.Click.ToString(), false);
            if (levelState == UConst.Level_State_Lock)
                return;

            if (levelId >= UConst.Developer_Level_Id)
            {
                UIManager.Instance.ShowFlyTip(Localization.Instance.GetString("ComeSoon"));
                //UIManager.Instance.GetPanel<MessageTipUI>(UConst.MessageTipUI_PrefabName)
                    //.ShowTip();
                return;
            }
            GameCtrl.Instance.playPanel.Show(levelId);
            Invoke("HideLevelSelectPanel", 1);
        });
    }



    public void Init(int _id)
    {
        levelId = _id;
        if (levelId == GameCtrl.Instance.maxLevelId)
        {
            levelState = UConst.Level_State_Ready;
        }
        else
        {
            levelState = GameCtrl.Instance.GetLevelState(levelId);
        }

        UpdateLevelItem();
    }

    private void UpdateLevelItem()
    {
        finishMarkImage.gameObject.SetActive(levelState == UConst.Level_State_Finish);
        levelText.text = levelId.ToString();
        bgImage.color = GetBgImageColor();

    }

    private Color GetBgImageColor()
    {
        if (levelState == UConst.Level_State_Lock)
            return lockColor;

        return unLockColor;
    }

    private void HideLevelSelectPanel()
    {
        GameCtrl.Instance.levelPanel.Hide();
    }
}
